﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using NetDon.OA.Interface.Admin;
using NetDon.Utilities;

namespace NetDon.OA.Portal.Controls
{
    public class CompanyDropDown: DropDownList
    {
        public CompanyDropDown()
            : base()
        {
            this.Items.Clear();
            this.Items.AddRange(GetItems());
            this.Items.Insert(0, new ListItem(EnumUtilities.GetDescription(ListEmptyType.Select),string.Empty));
        }

        public override string DataTextField
        {
            get { return "CompanyName"; }
            set { base.DataTextField = value; }
        }

        public override string DataValueField
        {
            get { return "CompanyId"; }
            set { base.DataValueField = value; }
        }

        public ListItem[] GetItems()
        {
            var postService = ServiceContainer.GetService<ICompanyService>();
            var rootPositions = postService.GetRootCompanies();
            List<CompanyData> newPostEnties = new List<CompanyData>();
            foreach (var rootPosition in rootPositions)
            {
                newPostEnties.Add(new CompanyData() { CompanyId = rootPosition.CompanyId, CompanyName = rootPosition.CompanyName });
                LoadPositions(rootPosition.CompanyId, newPostEnties);
            }

            ListItem[] lists = new ListItem[0];
            foreach (var postData in newPostEnties)
            {
                Array.Resize<ListItem>(ref lists, lists.Length + 1);
                lists[lists.Length - 1] = new ListItem(postData.CompanyName, postData.CompanyId.ToString());
            }
            return lists;
        }

        void LoadPositions(Guid parentId, List<CompanyData> newPostEntities)
        {
            var postService = ServiceContainer.GetService<ICompanyService>();
            var childPosts = postService.GetChildCompanies(parentId);
            foreach (var postEntity in childPosts)
            {
                newPostEntities.Add(new CompanyData() { CompanyId = postEntity.CompanyId, CompanyName = "|" + postEntity.CompanyName.PadLeft(postEntity.CompanyName.Length + 2 * (postEntity.NodeLevel.Value - 1), '-') });
                LoadPositions(postEntity.CompanyId, newPostEntities);
            }
        }
    }

    public class DeparmentDropDown : DropDownList
    {
        public DeparmentDropDown()
            : base()
        {
            this.Items.Clear();
        
            this.Items.Insert(0, new ListItem(EnumUtilities.GetDescription(ListEmptyType.Select), string.Empty));
        }

        public override string DataTextField
        {
            get { return "DepartmentName"; }
            set { base.DataTextField = value; }
        }

        public override string DataValueField
        {
            get { return "DepartmentId"; }
            set { base.DataValueField = value; }
        }

        public void LoadDepartments(Guid companyId)
        {
            this.Items.Clear();
            this.Items.AddRange(GetItems(companyId));
            this.Items.Insert(0, new ListItem(EnumUtilities.GetDescription(ListEmptyType.Select), string.Empty));
        }

        public ListItem[] GetItems(Guid companyId)
        {
            var postService = ServiceContainer.GetService<IDepartmentService>();
            var rootPositions = postService.GetRootDepartments(companyId);
            List<DepartmentData> newPostEnties = new List<DepartmentData>();
            foreach (var rootPosition in rootPositions)
            {
                newPostEnties.Add(new DepartmentData() { DepartmentId = rootPosition.DepartmentId, DepartmentName = rootPosition.DepartmentName });
                LoadPositions(rootPosition.DepartmentId, newPostEnties);
            }

            ListItem[] lists = new ListItem[0];
            foreach (var postData in newPostEnties)
            {
                Array.Resize<ListItem>(ref lists, lists.Length + 1);
                lists[lists.Length - 1] = new ListItem(postData.DepartmentName, postData.DepartmentId.ToString());
            }

            return lists;
        }

        void LoadPositions(Guid parentId, List<DepartmentData> newPostEntities)
        {
            var postService = ServiceContainer.GetService<IDepartmentService>();
            var childPosts = postService.GetChildDepartments(parentId);
            foreach (var postEntity in childPosts)
            {
                newPostEntities.Add(new DepartmentData() { DepartmentId = postEntity.DepartmentId, DepartmentName = "|" + postEntity.DepartmentName.PadLeft(postEntity.DepartmentName.Length + 2 * (postEntity.NodeLevel.Value - 1), '-') });
                LoadPositions(postEntity.DepartmentId, newPostEntities);
            }
        }
    }
}